<?php

/** Wrapper class to utilise both soIniData and dzStruct objects in order
 *  to overcome problems with alias table names possibly used in ini data.
 *
 *  @author Jyry Kuukkanen
 *  $Id: dzinifiles.php 595 2006-04-03 16:00:47Z jyry $
 */

include_once("sodebug.php");
include_once("soinifiles.php");
include_once("sostring.php");

include_once("dzconst.php");

/** Class to access ini data that converts alias table names into real
 *  names on the fly.
 */
class dzIniData extends soIniData {
    var $__Struct;  /** dzStruct Stucture to use. */


    /** Constructor
     *  >Struct:dzStruct Structure object to use when converting alias table
     *      names
     *  >$IniStream:str Stream string (filename/str)
     *  >$LineDelim:str Ini string line delim str.
     *  >$InCase:bool Set to true when searching case-insensitively.
    */
    function dzIniData(&$Struct, $IniStream, $LineDelim = "\n", $InCase = 0) {
        $this->__Struct = $Struct;
        $this->__InCase = $InCase;

        $this->readStream($IniStream, $LineDelim);
        $this->CurrentSectionName = "";
        $this->CurrentSectionNo = -1;
        $this->__CurrentSectionStartRow = -1;
    } // constructor


    /** Get an option value
     * This is dataxi specific getValue. It modifies the result strings
     * so that aliases are replaced with real table names.
     * >$Section:str    The section name where the value belongs to
     * >$Key:str    The key to the value to get
     */
    function getDzValue($Section, $Key) {
        switch ($Key) {
            case DZCP_FORMULA:
            case DZCP_AFTER_CHANGE:
                $result = $this->getDzFormula($Section, $Key);
                break;

            default:
                $result = $this->getValue($Section, $Key);
        } // switch

        return $result;
    } // getDzValue


    /** Gets an ini item and modifies formula notation
     *  Same as getValue, but the value returned is modified, so that
     *  $alias.column$ are replaced by $table.column$ strings.
     */
    function getDzFormula($Section, $Key) {
        $result = $this->getValue($Section, $Key);
        /* replace the alias names in the formula */
        $tag = soParseVal($result, "\$", "\$", $occurance = 1);
        while ($tag != NULL) {
            $real_tag = $this->__Struct->findCol($tag);
            if (!$real_tag)
                $real_tag = $tag;
            /* If the tag was modified update the formula also. */
            if (soStrPos($real_tag, ".") < 0) {
                $real_tag = DZPSEUDO.".".$real_tag;
            };
            $real_tag = soSwapStr($real_tag, ".", DZ_DR_STR);
            $result = str_replace($tag, $real_tag, $result);

            $occurance += 2;
            $tag = soParseVal($result, "\$", "\$", $occurance);
        };

        /* Insert quotes inside the brackets when dealing with sum function */
        $tag = soParseVal($result, "sum(\$", "\$)", 1);
        while ($tag != NULL) {
            $result = soSwapStr($result,
                                "sum(\$$tag\$)",
                                "sum('\$$tag\$')");
            $tag = soParseVal($result, "sum(\$", "\$)", 1);
        } // while


        return $result;
    } // getDzFormula


    /** Gets section keys (aliases converted)
     */
    function getDzSectionKeys($Sections) {
        $result = $this->getSectionKeys($Sections);
        $icount = count($result);
        for ($i = 0; $i < $icount; $i++) {
            $result[$i] = $this->__Struct->findCol($result[$i]);
        }; // for $i

        return $result;
    } // getDzSectionKeys


}; // dzIniData

?>
